import * as types from './mutation-types'
import * as $helpers from '../utils/helpers'

export function OPEN_REFUND_MODAL(state) {
  const { modal } = state
  state.modal = {
    ...modal,
    refund: true
  }
}

export function CLOSE_REFUND_MODAL(state) {
  const { modal } = state
  state.modal = {
    ...modal,
    refund: false
  }
}

export function UPDATE_ORDER(state, { order }) {
  state.order = order
}

export function UPDATE_ORDER_AMOUNT(state, { amount }) {
  state.order.TotalAmount = amount
}

export function UPDATE_INPUT_VALUE(state, { path, value }) {
  const newState = $helpers.setValueByPath(state, path, value)
  state = {
    ...state,
    ...newState
  }
}

export function updateInputValue(state, { value, path }) {
  let attr = ''
  if (!path.includes('.')) {
    attr = path
  } else {
    attr = path.split('.')[0]
  }
  if (!attr) {
    return
  }
  const newState = $helpers.setValueByPath(state, path, value)
  state[attr] = newState[attr]
}

export function updateInstalments(state, { instalments }) {
  state.instalments = instalments
}

export function updateInstalmentsWhenTotalAmountChanged(state, { totalAmount }) {
  const { instalments } = state
  const mapInstalment = (instalment) => {
    return {
      ...instalment,
      MayReceive: Math.ceil(instalment.Percent * totalAmount / 100),
    }
  }
  let newInstalments = instalments.map(mapInstalment)
  const restPay = calcRestPay({ total: totalAmount, instalments: newInstalments })
  newInstalments = newInstalments.map(calculateLastInstalment({ rest: restPay, type: 'MayReceive' }))
  state.instalments = newInstalments
}

export function UPDATE_INSTALMENTS_VIA_PERCENTAGE_BLURED(state, payload = {}) {
  const { order, instalments } = state
  const total = order.TotalAmount
  let newInstalments = instalments.map((instalment, index) => {
    if (index !== payload.index) {
      return instalment
    }
    return {
      ...instalment,
      MayReceive: Math.ceil(total * instalment.Percent / 100)
    }
  })
  newInstalments = autoCalcInstalments({ total, instalments: newInstalments })
  state.instalments = newInstalments
}

export function UPDATE_INSTALMENTS_VIA_MONEY_BLURED(state, payload = {}) {
  const { order, instalments } = state
  const total = order.TotalAmount
  let newInstalments = instalments.map((instalment, index) => {
    if (index !== payload.index) {
      return instalment
    }
    return {
      ...instalment,
      Percent: Math.ceil(instalment.MayReceive / total * 100)
    }
  })
  newInstalments = autoCalcInstalments({ total, instalments: newInstalments })
  state.instalments = newInstalments
}

function autoCalcInstalments({ total, instalments }) {
  const restPay = calcRestPay({ total, instalments })
  const restPercentage = calcRestPercentage({ instalments })
  return instalments.map((instalment, index, instalments) => {
    if (index !== instalments.length - 1) {
      return instalment
    }
    return {
      ...instalment,
      MayReceive: restPay,
      Percent: restPercentage
    }
  })
}

function calcRestPay({ total, instalments }) {
  const size = instalments.length
  return total - instalments
    .filter((_, index) => index < size - 1)
    .map(instalment => instalment.MayReceive)
    .reduce(add, 0)
}

function calcRestPercentage({ instalments }) {
  return 100 - instalments
    .filter((_, index, arr) => index < arr.length - 1)
    .map(instalment => instalment.Percent)
    .reduce(add, 0)
}

const calculateLastInstalment = ({ rest, type }) => (instalment, index, instalments) => {
  if (index !== instalments.length - 1) {
    return instalment
  }
  return {
    ...instalment,
    [type]: rest
  }
}

function add(a, b) {
  return a + b
}
